﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AimGameLibrary;

using AimGameEngine.EditorObject;

namespace AimGameEngine.NodeEditorObject
{
    public class Relational
    {
       const String Equality = "==";
       const String Inequality = "!=";
       const String LessThan = "<";
       const String GreaterThan = ">";
       const String LessThanOrEqualTo = "<=";
       const String GreaterThanOrEqualTo = ">=";
       
       

       List<String> listrelationl;
       public Relational()
       {
           listrelationl = new List<string>();
           listrelationl.Add(Equality);
           listrelationl.Add(Inequality);
           listrelationl.Add(LessThan);
           listrelationl.Add(GreaterThan);
           listrelationl.Add(LessThanOrEqualTo);
           listrelationl.Add(GreaterThanOrEqualTo);
           
       }

       public List<String> GetrelationL { get { return listrelationl; } }
    }

    public class NodeIfelse : NodeBaseControl
    {
        public NodeObject nodeObject1;
        public NodeObject nodeObject2;
        public String SelectRelational = "==";
        public String Result = "NULL";
        public Dictionary<String, NodeObject> DictionaryObjectIf;
        public Dictionary<String, NodeObject> DictionaryObjectElse;

        public NodeIfelse(SpriteBatch inputspritebatch, Camera inputcamera, NodeData inputData
            , List<IDraw> MyListDraw, List<IUpdateMainTime> MyListUpdateTime, Dictionary<String, NodeObject> MyListObject, Dictionary<String, NodeObject> MyListType)
            : base(inputspritebatch, inputcamera, inputData, MyListDraw,MyListUpdateTime, MyListObject, MyListType)
	    {
            DictionaryObjectIf = new Dictionary<string, NodeObject>();
            DictionaryObjectElse = new Dictionary<string, NodeObject>();

            Connecter outputUpdate = new Connecter(inputspritebatch, inputcamera, Syco.Output, NodeTypeValue.Update, this);
            ListConnectpoint.Add(outputUpdate);

            Connecter inputValue1 = new Connecter(inputspritebatch, inputcamera, Syco.Input, NodeTypeValue.Group_Value, this);
            ListConnectpoint.Add(inputValue1);

            Connecter inputValue2 = new Connecter(inputspritebatch, inputcamera, Syco.Input, NodeTypeValue.Group_Value, this);
            ListConnectpoint.Add(inputValue2);

            Connecter inputUpdateIf = new Connecter(inputspritebatch, inputcamera, Syco.Input, NodeTypeValue.Update, this);
            ListConnectpoint.Add(inputUpdateIf);

            Connecter inputUpdateElse = new Connecter(inputspritebatch, inputcamera, Syco.Input, NodeTypeValue.Update, this);
            ListConnectpoint.Add(inputUpdateElse);
        }

        public override void SetupInput(Connecter Thisconnect, Connecter connectOutput)
        {
                switch (ListConnectpointInput.IndexOf(Thisconnect))
                {
                    case 0: nodeObject1 = connectOutput.MyNode;
                            LogicDetect(nodeObject2);
                        break;
                    case 1: nodeObject2 = connectOutput.MyNode;
                            LogicDetect(nodeObject1);                  
                        break;
                    case 2: DictionaryObjectIf.Add(connectOutput.MyNode.MyData.MyName, connectOutput.MyNode); break;
                    case 3: DictionaryObjectElse.Add(connectOutput.MyNode.MyData.MyName, connectOutput.MyNode); break;
                }
        }

        void LogicDetect(NodeObject inputnodeobject)
        {
            if (inputnodeobject != null)
            {
                if (inputnodeobject.GetType() == typeof(NodeKeyController))
                {
                    foreach (LinkLine line in inputnodeobject.LinklinetoConnecter)
                        if (CheckDeleteLink(line, NodeTypeValue.Group_Value))
                        {
                            line.Isconnect = false; 
                            break;
                        }

                    inputnodeobject = null;
                }
            }
        }
        bool CheckDeleteLink(LinkLine inputlinkline, NodeTypeValue typevalue)
        {
            if ((inputlinkline.StartConnecter.MyType == typevalue && inputlinkline.StartConnecter.MyNode.MyData.MyName == this.MyData.MyName) ||
                (inputlinkline.EndConnecter.MyType == typevalue && inputlinkline.EndConnecter.MyNode.MyData.MyName == this.MyData.MyName))
                return true;
            else return false;
        }

        public override void ClearInput(Connecter ThisInputConnect, Connecter ThisConnectClear, string NameThisClear)
        {
            switch (ListConnectpointInput.IndexOf(ThisInputConnect))
            {
                case 0: nodeObject1 = null;  break;
                case 1: nodeObject2 = null;  break;
                case 2: DictionaryObjectIf.Remove(ThisConnectClear.MyNode.MyData.MyName); break;
                case 3: DictionaryObjectElse.Remove(ThisConnectClear.MyNode.MyData.MyName); break;
            }
        }

        public override void ShowProprety(ArrayList Temp, CallBack input)
        {
            base.ShowProprety(Temp, input);
            PropretyIfelse propretyif = new PropretyIfelse(this);
            //propretyif.Deactivate += new EventHandler(input.Deactivate);
            propretyif.okButton.Click +=new EventHandler(input.Deactivate);
            propretyif.ShowDialog();
            
        }

        protected override Dictionary<string, NodeObject> GetDictionaryNode(Dictionary<string, NodeObject> TempNode)
        {
            if (this.nodeObject1 != null)
                if (this.MyDictionaryObject.ContainsKey(nodeObject1.MyData.MyName))
                    TempNode.Add(nodeObject1.MyData.MyName, this.MyDictionaryObject[nodeObject1.MyData.MyName]);

            if(this.nodeObject2 != null)
                if (this.MyDictionaryObject.ContainsKey(nodeObject2.MyData.MyName))
                    TempNode.Add(nodeObject2.MyData.MyName, this.MyDictionaryObject[nodeObject2.MyData.MyName]);

            foreach (String nameIf in DictionaryObjectIf.Keys)
                if (this.MyDictionaryObject.ContainsKey(nameIf))
                    TempNode.Add(nameIf, this.MyDictionaryObject[nameIf]);

            foreach (String nameIf in DictionaryObjectElse.Keys)
                if (this.MyDictionaryObject.ContainsKey(nameIf))
                    TempNode.Add(nameIf, this.MyDictionaryObject[nameIf]);

            return base.GetDictionaryNode(TempNode);
        }

        public override void SaveData(System.Collections.ArrayList Thislist, SaveMode inputsavemode)
        {
            base.SaveData(Thislist,inputsavemode);

            Thislist.Add(SelectRelational);
            Thislist.Add(Result);

            if (nodeObject1 != null)
                Thislist.Add(nodeObject1.MyData.MyName);
            else Thislist.Add("NULL");

            if (nodeObject2 != null)
                Thislist.Add(nodeObject2.MyData.MyName);
            else Thislist.Add("NULL");

            Thislist.Add(DictionaryObjectIf.Count);
            if (DictionaryObjectIf.Count > 0)
                Thislist.Add(CreateData(DictionaryObjectIf));
            else Thislist.Add("NULL");

            Thislist.Add(DictionaryObjectElse.Count);
            if (DictionaryObjectElse.Count > 0)
                Thislist.Add(CreateData(DictionaryObjectElse));
            else Thislist.Add("NULL");
        }

        public override void LoadData(Identification getdata, int IndexData)
        {
            base.LoadData(getdata, IndexData);

            if (getdata.Items.GetValue(IndexData + 1).GetType() == typeof(String))
            SelectRelational = (String)getdata.Items.GetValue(IndexData + 1);

            if (getdata.Items.GetValue(IndexData + 2).GetType() == typeof(String))
            Result = (String)getdata.Items.GetValue(IndexData + 2);
        }
    }
}
